package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.controller.form.ProductCatForm;
import com.woniuxy.controller.info.ProductCatInfo;
import com.woniuxy.controller.info.ProductCatXInfo;
import com.woniuxy.dao.*;
import com.woniuxy.exception.CheckedIsNullException;
import com.woniuxy.exception.DdException;
import com.woniuxy.exception.ProductCatNameExsitsException;
import com.woniuxy.exception.ProductCatNotExsitsException;
import com.woniuxy.model.*;
import com.woniuxy.service.PawnProductCatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.utils.NewCopyBean;
import com.woniuxy.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hujianying
 * @since 2022-03-23
 */
@Service
public class PawnProductCatServiceImpl extends ServiceImpl<PawnProductCatDao, PawnProductCat> implements PawnProductCatService {

    @Resource
    private PawnAttrGroupCatRelDao pagcrd;

    @Resource
    private PawnBrandCatRelDao pbcrd;

    @Resource
    private PawnAttrGroupDao pagd;

    @Resource
    private PawnAttrConfDao pacd;

    @Resource
    private PawnGoodsAttrRelDao pgard;

    @Resource
    private PawnGoodsDao pgd;
    /**
     * 大类分页展示
     * @param pcf 分页参数
     * @return data为productCatInfoPage
     */
    @Override
    public Result getAllProductCat(ProductCatForm pcf) {
        //查询大类条件
        QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<>();
        wrapper.eq("p_cat_code",0);
        Page<PawnProductCat> productCatForm = baseMapper.selectPage(pcf,wrapper);
        Page<ProductCatInfo> productCatInfoPage = new Page<>();
        List<ProductCatInfo> productCatInfos = NewCopyBean.copyList(productCatForm.getRecords(), ProductCatInfo.class);
        productCatInfoPage.setRecords(productCatInfos);
        productCatInfoPage.setCurrent(productCatForm.getCurrent());
        productCatInfoPage.setSize(productCatForm.getSize());
        productCatInfoPage.setTotal(productCatForm.getTotal());
        return Result.success(200,"查询成功",productCatInfoPage);
    }

    /**
     * 商品大类模糊查询
     * @param pcf 中模糊查询参数
     * @return data为pawnProductCatPage
     */
    @Override
    public Result getProductCatByLike(ProductCatForm pcf) throws DdException {
//        if(null!=pcf.getStr()&&pcf.getStr()!=""){
            QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<>();
            wrapper.like("cat_name","%" + pcf.getStr() + "%");
            wrapper.eq("p_cat_code",0);
            Page<PawnProductCat> productCatForm = baseMapper.selectPage(pcf, wrapper);
            Page<ProductCatInfo> pawnProductCatPage = new Page<>();
            if(ObjectUtils.isEmpty(productCatForm.getRecords())){
                throw new ProductCatNotExsitsException(50001,"未查询到分类");
            }else{
                List<ProductCatInfo> productCatInfos = NewCopyBean.copyList(productCatForm.getRecords(), ProductCatInfo.class);
                pawnProductCatPage.setRecords(productCatInfos);
                pawnProductCatPage.setTotal(productCatForm.getTotal());
                pawnProductCatPage.setSize(productCatForm.getSize());
                pawnProductCatPage.setCurrent(productCatForm.getCurrent());
                return Result.success(200,"查询成功",pawnProductCatPage);
            }
//        }else{
//            throw new ProductCatNotExsitsException(50001,"未输入查询信息");
//        }
    }

    /**
     * 商品大类新增
     * @param pcf 商品分类相关信息
     * @return data为null
     */
    @Override
    @Transactional
    public Result saveProductCat(ProductCatForm pcf) throws DdException{
        PawnProductCat catName = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", pcf.getCatName()));
        if(ObjectUtils.isEmpty(catName)){
            PawnProductCat pawnProductCat = NewCopyBean.copyOne(pcf, PawnProductCat.class);
            pawnProductCat.setCatLvl(1);
            pawnProductCat.setPCatCode("0");
            baseMapper.insert(pawnProductCat);
            PawnProductCat catName2  = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name",pcf.getCatName()));
            pawnProductCat.setCatRoute(catName2.getCatCode().toString());
            baseMapper.updateById(pawnProductCat);
            return Result.success(200,"新增大类成功",null);
        }else{
            throw new ProductCatNameExsitsException(50002,"该类别已存在");
        }
    }

    /**
     * 商品大类修改
     * @param pcf 商品分类相关信息，根据分类id修改
     * @return data为null
     */
    @Override
    @Transactional
    public Result updateProductCat(ProductCatForm pcf)throws DdException{
        QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<PawnProductCat>().eq("cat_name", pcf.getCatName());
        PawnProductCat productCat = baseMapper.selectOne(wrapper);
        if(ObjectUtils.isEmpty(productCat)||productCat.getCatCode()==pcf.getCatCode()){
            pcf.setCatLvl(1);
            pcf.setCatRoute(pcf.getCatCode().toString());
            pcf.setPCatCode("0");
            PawnProductCat pawnProductCat = NewCopyBean.copyOne(pcf, PawnProductCat.class);
            baseMapper.update(pawnProductCat,new QueryWrapper<PawnProductCat>().eq("cat_code",pcf.getCatCode()));
            return Result.success(200,"修改成功",null);
        }else{
            throw new ProductCatNameExsitsException(50002,"该类别已存在");
        }
    }

    /**
     * 商品大类删除,同时删除对应的小类
     * @param pcf 根据分类id删除
     * @return data为null
     */
    @Override
    @Transactional
    public Result deleteProductCat(ProductCatForm pcf)throws DdException {
        List<ProductCatInfo> isChecked = pcf.getIsChecked();
        if(ObjectUtils.isEmpty(isChecked)){
            throw new CheckedIsNullException(50009,"请先勾选删除");
        }else{
            for(ProductCatInfo s : isChecked){
                List<PawnGoods> cat_code = pgd.selectList(new QueryWrapper<PawnGoods>().eq("cat_code", s.getCatCode()));
                if(!ObjectUtils.isEmpty(cat_code)){
                    throw new CheckedIsNullException(50009,"勾选类中已有商品");
                }
            }
            for(ProductCatInfo p : isChecked){
                baseMapper.delete(new QueryWrapper<PawnProductCat>().eq("p_cat_code",p.getCatCode()));
                baseMapper.delete(new QueryWrapper<PawnProductCat>().eq("cat_code",p.getCatCode()));
                //删除属性组与分类关联表
                List<PawnAttrGroupCatRel> catRels = pagcrd.selectList(new QueryWrapper<PawnAttrGroupCatRel>().eq("cat_code", p.getCatCode()));
                for(PawnAttrGroupCatRel p1 : catRels){
                    //删除属性组表和属性参数表
                    pagd.deleteById(p1.getGroupCode());
                }
                pagcrd.delete(new QueryWrapper<PawnAttrGroupCatRel>().eq("cat_code",p.getCatCode()));
                //删除品牌分类关联表
                pbcrd.delete(new QueryWrapper<PawnBrandCatRel>().eq("cat_code",p.getCatCode()));
            }
            return Result.success(200,"删除成功",null);
        }

    }

    /**
     * 根据catCode查询
     * @param pcf
     * @return ProductCat对象
     */
    @Override
    public Result getProductCatByCatCode(ProductCatForm pcf) {
        PawnProductCat productCat = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", pcf.getCatCode()));
        ProductCatInfo productCatInfo = NewCopyBean.copyOne(productCat, ProductCatInfo.class);
        return Result.success(200,"查询成功",productCatInfo);
    }

    /**
     * 查全部小类
     * @param pcf 分页参数
     * @return 返回一个小类的info
     */
    @Override
    public Result getAllXProductCat(ProductCatForm pcf) {
        //查询所有小类
        QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<PawnProductCat>().ne("p_cat_code", 0);
        Page<PawnProductCat> productCat = baseMapper.selectPage(pcf, wrapper);
        List<PawnProductCat> records = productCat.getRecords();
        List<ProductCatXInfo> productCatXInfos = new ArrayList<>();
        for(PawnProductCat p :records){
            ProductCatXInfo productCatXInfo = NewCopyBean.copyOne(p, ProductCatXInfo.class);
            //根据父类code查询父类名称
            PawnProductCat cat_code = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", p.getPCatCode()));
            //设置父类名称
            productCatXInfo.setPcatName(cat_code.getCatName());
            productCatXInfos.add(productCatXInfo);
        }
        Page<ProductCatXInfo> productCatInfoPage = new Page<>();
        productCatInfoPage.setRecords(productCatXInfos);
        productCatInfoPage.setCurrent(productCat.getCurrent());
        productCatInfoPage.setTotal(productCat.getTotal());
        productCatInfoPage.setSize(productCat.getSize());
        return Result.success(200,"查询成功",productCatInfoPage);
    }

    /**
     * 根据大类查对应全部小类
     * @param pcf 获取大类类型
     * @return 返回小类集合
     */
    @Override
    public Result getXProductCatByD(ProductCatForm pcf) {
        Page<PawnProductCat> productCatForm = baseMapper.selectPage(pcf, new QueryWrapper<PawnProductCat>().eq("p_cat_code", pcf.getCatCode()));
        Page<ProductCatXInfo> productCatXInfoPage = new Page<>();
        List<PawnProductCat> records = productCatForm.getRecords();
        List<ProductCatXInfo> productCatXInfos = new ArrayList<>();
        for(PawnProductCat p : records){
            ProductCatXInfo productCatXInfo = NewCopyBean.copyOne(p, ProductCatXInfo.class);
            PawnProductCat cat_code = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", p.getPCatCode()));
            productCatXInfo.setPcatName(cat_code.getCatName());
            productCatXInfos.add(productCatXInfo);
        }
        productCatXInfoPage.setRecords(productCatXInfos);
        productCatXInfoPage.setTotal(productCatForm.getTotal());
        productCatXInfoPage.setSize(productCatForm.getSize());
        productCatXInfoPage.setCurrent(productCatForm.getCurrent());

        return Result.success(200,"查询成功",productCatXInfoPage);
    }

    /**
     * 小类模糊查询
     * @param pcf 小类模糊查询参数str
     * @return 返回小类集合
     */
    @Override
    public Result getXProductCatByLike(ProductCatForm pcf) throws DdException{
        QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<>();
        wrapper.like("cat_name","%" + pcf.getStr() + "%");
        wrapper.ne("p_cat_code",0);
        Page<PawnProductCat> productCatForm = baseMapper.selectPage(pcf, wrapper);
        if(ObjectUtils.isEmpty(productCatForm.getRecords())){
            throw new ProductCatNotExsitsException(50001,"未查询到分类");
        }else{
            Page<ProductCatXInfo> productCatXInfoPage = new Page<>();
            List<ProductCatXInfo> productCatXInfos = new ArrayList<>();
            List<PawnProductCat> records = productCatForm.getRecords();
            for(PawnProductCat p : records){
                ProductCatXInfo productCatXInfo = NewCopyBean.copyOne(p, ProductCatXInfo.class);
                PawnProductCat cat_code = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", p.getPCatCode()));
                productCatXInfo.setPcatName(cat_code.getCatName());
                productCatXInfos.add(productCatXInfo);
            }
            productCatXInfoPage.setCurrent(productCatForm.getCurrent());
            productCatXInfoPage.setSize(productCatForm.getSize());
            productCatXInfoPage.setTotal(productCatForm.getTotal());
            productCatXInfoPage.setRecords(productCatXInfos);
            return Result.success(200,"查询成功",productCatXInfoPage);
        }
    }
    /**
     * 根据分类模糊查询
     * @param pcf 大类名和模糊关键字
     * @return 返回小类集合
     */
    @Override
    public Result getXProductCatByDAndLike(ProductCatForm pcf)throws DdException {
        QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<>();
        wrapper.eq("p_cat_code", pcf.getCatCode());
        wrapper.like("cat_name","%" + pcf.getStr() + "%");
        Page<PawnProductCat> productCatForm = baseMapper.selectPage(pcf, wrapper);
        List<PawnProductCat> records = productCatForm.getRecords();
        if(ObjectUtils.isEmpty(records)){
            throw new ProductCatNotExsitsException(50001,"未查询到分类");
        }else{
            List<ProductCatXInfo> productCatXInfos = new ArrayList<>();
            Page<ProductCatXInfo> productCatXInfoPage = new Page<>();
            for(PawnProductCat p : records){
                ProductCatXInfo productCatXInfo = NewCopyBean.copyOne(p, ProductCatXInfo.class);
                PawnProductCat cat_code = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", p.getPCatCode()));
                productCatXInfo.setPcatName(cat_code.getCatName());
                productCatXInfos.add(productCatXInfo);
            }
            productCatXInfoPage.setSize(productCatForm.getSize());
            productCatXInfoPage.setCurrent(productCatForm.getCurrent());
            productCatXInfoPage.setTotal(productCatForm.getTotal());
            productCatXInfoPage.setRecords(productCatXInfos);
            return Result.success(200,"查询成功",productCatXInfoPage);
        }
    }

    /**
     * 小类批量删除
     * @param pcf 根据小类catCode删除
     * @return data为null
     */
    @Override
    @Transactional
    public Result deleteXProductCat(ProductCatForm pcf) throws DdException{
        List<ProductCatInfo> isChecked = pcf.getIsChecked();
        if(ObjectUtils.isEmpty(isChecked)){
            throw new CheckedIsNullException(50009,"请先勾选删除");
        }else{
            for(ProductCatInfo s : isChecked){
                List<PawnGoods> sub_cate_code = pgd.selectList(new QueryWrapper<PawnGoods>().eq("sub_cate_code", s.getCatCode()));
                if(!ObjectUtils.isEmpty(sub_cate_code)){
                    throw new CheckedIsNullException(50009,"勾选类中已有商品");
                }
            }
            for(ProductCatInfo p :isChecked){
                //删除小类
                baseMapper.delete(new QueryWrapper<PawnProductCat>().eq("cat_code",p.getCatCode()));
            }
            return Result.success(200,"删除成功",null);
        }
    }

    /**
     * 新增小类
     * @param pcf 小类对象
     * @return data为null
     */
    @Override
    @Transactional
    public Result saveXProductCat(ProductCatForm pcf) throws DdException{
        //根据pcatName查询
        PawnProductCat cat_name = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", pcf.getPcatName()));
        //根据名字查询该大类下是否有重复
        QueryWrapper<PawnProductCat> wrapper = new QueryWrapper<>();
        wrapper.eq("p_cat_code",cat_name.getCatCode());
        List<PawnProductCat> pawnProductCats = baseMapper.selectList(wrapper);
        for(PawnProductCat p :pawnProductCats){
            if(p.getCatName().equals(pcf.getCatName())){
                throw new ProductCatNameExsitsException(50002,"该大类下小类已存在");
            }
        }
        //如果没有重复
        PawnProductCat pawnProductCat = NewCopyBean.copyOne(pcf, PawnProductCat.class);
        pawnProductCat.setPCatCode(cat_name.getCatCode().toString());
        pawnProductCat.setCatLvl(2);
        baseMapper.insert(pawnProductCat);
        PawnProductCat cat_name1 = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", pcf.getCatName()));
        pawnProductCat.setCatRoute(cat_name.getCatCode().toString()+","+ cat_name1.getCatCode().toString());
        baseMapper.update(pawnProductCat,new QueryWrapper<PawnProductCat>().eq("cat_name",pawnProductCat.getCatName()));
        return Result.success(200,"新增成功",null);
    }

    /**
     * 根据小类catCode查询所有信息
     * @param pcf catCode
     * @return data为ProductCatXInfo
     */
    @Override
    public Result getXProductByCatCode(ProductCatForm pcf) {
        PawnProductCat productCat = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", pcf.getCatCode()));
        ProductCatXInfo productCatXInfo = NewCopyBean.copyOne(productCat, ProductCatXInfo.class);
        //根据p_cat_code查对应的pcatName
        PawnProductCat cat_code = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_code", productCat.getPCatCode()));
        productCatXInfo.setPcatName(cat_code.getCatName());
        //返回大类的数据
        productCatXInfo.setEvalPicDef(cat_code.getEvalPicDef());
        return Result.success(200,"请求成功",productCatXInfo);
    }

    /**
     * 修改小类信息
     * @param pcf 修改信息
     * @return data为null
     */
    @Override
    @Transactional
    public Result updataXProductCat(ProductCatForm pcf){
        //查询对应的大类
        PawnProductCat cat_name = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", pcf.getPcatName()));
        PawnProductCat pawnProductCat = NewCopyBean.copyOne(pcf, PawnProductCat.class);
        pawnProductCat.setPCatCode(cat_name.getCatCode().toString());
        pawnProductCat.setCatRoute(cat_name.getCatCode().toString()+","+pcf.getCatCode());
        pawnProductCat.setCatLvl(2);
        baseMapper.updateById(pawnProductCat);
        return Result.success(200,"修改成功",null);
    }

    /**
     * 根据cat_name查询
     * @param pcf catName
     * @return ProductCat
     */
    @Override
    public Result getDProductCatByCatName(ProductCatForm pcf) {
        PawnProductCat cat_name = baseMapper.selectOne(new QueryWrapper<PawnProductCat>().eq("cat_name", pcf.getCatName()));
        ProductCatInfo productCatInfo = new ProductCatInfo();
        productCatInfo.setEvalPicDef(cat_name.getEvalPicDef());
        return Result.success(200,"请求成功",productCatInfo);
    }
}
